//
//  KYSlideView.m
//  KYSlide
//
//  Created by wesleyxiao on 14-3-20.
//  Copyright (c) 2014年 wesleyxiao. All rights reserved.
//

#import "KYSlideView.h"

@implementation KYSlideView
@synthesize array=_array,contents=_contents;
@synthesize scrollView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initScrollView];
    }
    return self;
}
-(void) initScrollView
{
    // Drawing code
    self.backgroundColor = [UIColor redColor];
    self.scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
    self.scrollView.backgroundColor = [UIColor yellowColor];
    [self addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.frame) * 3, CGRectGetHeight(self.frame));
    self.scrollView.pagingEnabled = YES;    //设置为N0的话，就为任意滑动
    self.scrollView.showsHorizontalScrollIndicator = YES;
    self.scrollView.showsVerticalScrollIndicator = YES;
    self.scrollView.delegate = self;
    
    
    self.array =[[NSMutableArray alloc]init];
    for (int i =0; i<3; i++)
    {
        UIView * tmpV = [[UIView alloc]init];
        UIColor * tmpColor;
        
        if (i==0)
            tmpColor = [UIColor blackColor];
        else if (i ==1)
            tmpColor = [UIColor redColor];
        else
            tmpColor = [UIColor greenColor];
        
        tmpV.backgroundColor = tmpColor;
       
        tmpV.frame = CGRectMake(i*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
        
        [self.array addObject:tmpV];
        [self.scrollView addSubview:tmpV];
    }
    self.contents = [[NSMutableArray alloc] init];
    for (int i = 0; i < 3; i ++)
    {
        [self.contents addObject:[NSNull null]];
    }

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
 */
- (void)drawRect:(CGRect)rect
{
    [self initScrollView];
    [self reloadData];
}
- (void)setOffsetRadio:(CGFloat)offsetRadio
{
    if (_offsetRadio != offsetRadio)
    {
        _offsetRadio = offsetRadio;
        
        //此时向右滑动超过一半了，需要翻页
        if (offsetRadio > 0.5)
        {
            _offsetRadio = offsetRadio - 1;
            self.activeIndex = [self validIndexValue: self.activeIndex + 1];
        }
        
        //此时向左滑动超过一半了，需要翻页
        if (offsetRadio < -0.5)
        {
            _offsetRadio = offsetRadio + 1;
            self.activeIndex = [self validIndexValue: self.activeIndex - 1];
        }
    }
}

- (void)setActiveIndex:(NSInteger)activeIndex
{
    if (_activeIndex != activeIndex)
    {
        _activeIndex = activeIndex;
        //每次页码一旦改变，需要重新调整下数据
        [self reloadData];
    }
}
- (NSInteger)validIndexValue:(NSInteger)value
{
    int numberOfControllers = 3;
    if(value == -1)
    {
        value = numberOfControllers - 1;
    }
    if(value == numberOfControllers)
    {
        value = 0;
    }
    return value;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
     self.offsetRadio =  _scrollView.contentOffset.x/CGRectGetWidth(_scrollView.frame) - 1;
}

- (void)reloadData
{
    NSArray *subViews = [self.scrollView subviews];
    //将所有的子view移出父视图，重新排列
    if([subViews count] != 0)
    {
        [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    /*
        scroll 的长度只有width 的三倍长，只需要替换 activeIndex 及其左右 三个就行了
        如：activeIndex         放在 1 位置
                activeIndex -1 则放在0 位置
                activeIndex +1则放在2 位置
     */
    for (int i = 0; i < 3; i ++)
    {
        NSInteger thisPage = [self validIndexValue: self.activeIndex - 1 + i];
        [self.contents replaceObjectAtIndex: i withObject:[self.array objectAtIndex:thisPage]];
    }
    
    for (int i = 0; i < 3; i++)
    {
        UIView* view =  [self.contents objectAtIndex:i];
        view.userInteractionEnabled = YES;
        view.frame = self.bounds;
        view.frame = CGRectOffset(self.scrollView.frame, view.frame.size.width * i, 0);
        [self.scrollView addSubview: view];
    }
    
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width + self.scrollView.frame.size.width * self.offsetRadio, 0)];
}

@end
